﻿Imports System.Data.OleDb

Public Class Usuario

    Private conexao As OleDbConnection
    Private dataReader As OleDbDataReader
    Private cmd As OleDbCommand

    Private _codigo As Integer
    Private _nome As String
    Private _senha As String
    Private _nivel As Integer

    Sub New()

    End Sub

    Public Property Codigo() As Integer
        Get
            Return _codigo
        End Get
        Set(ByVal value As Integer)
            _codigo = value
        End Set
    End Property

    Sub New(ByVal nome As String, ByVal senha As String)
        _nome = nome
        _senha = senha
    End Sub

    Public Property Nome() As String
        Get
            Return _nome
        End Get
        Set(ByVal value As String)
            _nome = value
        End Set
    End Property

    Public Property Senha() As String
        Get
            Return _senha
        End Get
        Set(ByVal value As String)
            _senha = value
        End Set
    End Property


    Public Property Nivel() As Integer
        Get
            Return _nivel
        End Get
        Set(ByVal value As Integer)
            _nivel = value
        End Set
    End Property

    Public Function login() As Boolean

        Dim sqlCmd As String = String.Format("SELECT * FROM tblLogin WHERE usuarioLog = '{0}' AND senhaLog = '{1}'" _
                                             , Me.Nome _
                                             , WiseLibraries.Crypto.CryptoFactory.makeCryptographer(WiseLibraries.SortingAlgorithm.Rijndael).Encrypt(Me.Senha))

        Try
            conexao = FabricaDeConexoes.CriarConexao()
            conexao.Open()
            cmd = New OleDbCommand(sqlCmd, conexao)
            dataReader = cmd.ExecuteReader()

            If (dataReader.HasRows) Then
                dataReader.Read()
                Me.Nivel = dataReader("codnivelUsuario")
                Me.Codigo = dataReader("codLogin")
                Return True
            Else
                Return False
            End If
        Catch ex As OleDbException
            Debug.Write(ex.Message)
        Finally
            conexao.Close()
        End Try

        Return (False)

    End Function

    Public Function buscarPorEmail(ByVal email As String) As Usuario
        Dim u As Usuario = Nothing

        Dim sqlCmd As String = String.Format("SELECT * FROM tblLogin WHERE emailUsuario = '{0}'", email)

        Try
            conexao = FabricaDeConexoes.CriarConexao()
            conexao.Open()
            cmd = New OleDbCommand(sqlCmd, conexao)
            dataReader = cmd.ExecuteReader()

            If (dataReader.HasRows) Then
                dataReader.Read()
                u = New Usuario()
                u._codigo = dataReader("codLogin")
                u.Nome = dataReader("usuarioLog")
                u.Senha = WiseLibraries.Crypto.CryptoFactory.makeCryptographer(WiseLibraries.SortingAlgorithm.Rijndael).Decrypt(dataReader("senhaLog"))
                u.Nivel = dataReader("codnivelUsuario")
            End If
        Catch ex As OleDbException
            Debug.WriteLine(ex.Message)
        End Try

        Return (u)
    End Function
End Class